package com.naiterui.ehp.bs.emr.repository.mongo;

import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.naiterui.common.repository.core.BaseMongoRepository;
import com.naiterui.ehp.bs.emr.entity.mongo.PatientMedicalRecord;
import com.naiterui.ehp.bs.emr.utils.Constants;

/**
 * @author wangzehao
 * @date 2018/10/17 11:02 AM
 * @since 1.0.0
 */
@Repository
public interface PatientMedicalRecordRepository extends BaseMongoRepository<PatientMedicalRecord, String> {

    /**
     * 根据医生病历ID获取推送患者的病历信息
     *
     * @param drRecordId 医生病历ID
     *
     * @return
     */
    PatientMedicalRecord findByDrRecordId(String drRecordId);

    @Query("{ 'patientId' : ?#{#patientId} , 'status' : " + Constants.CASE_STATUS_FINISH + " }")
    Page<PatientMedicalRecord> getCasePageByPatientId(@Param("patientId") Long patientId, Pageable pageable);

    @Query("{ 'patientId' : ?#{#patientId} ,'doctorId' : ?#{#doctorId} ,'source' : " + Constants.CASE_SOURCE_DOCTOR + ", 'status' : " + Constants.CASE_STATUS_FINISH + " }")
    Page<PatientMedicalRecord> getCasePageByPatientAndDoctorId(@Param("patientId") Long patientId, @Param("doctorId") Long doctorId, Pageable pageable);

    PatientMedicalRecord findByIdAndPatientId(@Param("id") String id, @Param("patientId") Long patientId);

    PatientMedicalRecord findByPatientIdAndFamilyIdAndStatus(@Param("patientId") Long patientId,
                                                             @Param("familyId") Long familyId,
                                                             @Param("status") Integer status);

    @Query("{ 'recordScale.extBizId' : ?#{#extBizId} }")
    Optional<PatientMedicalRecord> findByExtBizId(@Param("extBizId") String extBizId);
}
